import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;



public class MyFrame extends JFrame implements ActionListener {

	JMenuBar mb = new JMenuBar();
	JFileChooser fileChooser = new JFileChooser();
	File file;
	BufferedImage image = null;
	int height ,width;

	
	public MyFrame()
	{
		fileChooserSettings(); 		//applies various fileChooser settings
		addFileMenu();				//Adding the fileMenu
		frameSettings();			//applies various frame settings
		
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("Open image"))
		{
			int ret = fileChooser.showDialog(null, "Open image");
			if(ret == JFileChooser.APPROVE_OPTION)
			{
				loadImage();	
			}	
		}
		if(e.getActionCommand().equals("Close image"))
		{
			JOptionPane.showMessageDialog(null, "This button isn't doing anything right now :D", "Error", JOptionPane.ERROR_MESSAGE);
		}
		if(e.getActionCommand().equals("Exit"))
		{
			System.exit(0);
		}
		
	}
	public void loadImage() 
	{
		try
		{
			file = fileChooser.getSelectedFile();
			image = ImageIO.read(file);
			height = image.getHeight();
			width = image.getHeight();
			
			
			
		}catch (Exception e1) {
			JOptionPane.showMessageDialog(null, "Please open an image file", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		g.drawImage(image, width, height, this);
		
	}
	
	
	private void addFileMenu()
	{
		//fileMenu
		JMenu fileMenu = new JMenu("File");
		mb.add(fileMenu);
		
		//JMenuItems
		JMenuItem open = new JMenuItem("Open image");
		JMenuItem close = new JMenuItem("Close image");
		JMenuItem exit = new JMenuItem("Exit");
		
		//Adding items to fileMenu
		fileMenu.add(open);
		fileMenu.add(close);
		fileMenu.add(exit);
		
		//Adding actionlisteners
		open.addActionListener(this);
		close.addActionListener(this);
		exit.addActionListener(this);
	}
	
	private void fileChooserSettings()
	{
		FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "jpg", "gif", "jpeg", "psd");
		fileChooser.setFileFilter(filter);
	}
	
	private void frameSettings()
	{
		setTitle("Graphics");
		setSize(1000, 1000);
		setResizable(false);
		setJMenuBar(mb);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
	}
	
	
}
